//
//  OCSSOrderPage.swift
//  OCSSStoreSwift
//
//  Created by MacMiniKangChe on 2018/10/11.
//  Copyright © 2018年 MacMiniKangChe. All rights reserved.
//

import UIKit
import SwiftyJSON
import HandyJSON

class OCSSOrderPage: OCSSBaseRefreshViewController,UITableViewDataSource,UITableViewDelegate {

    var orderData:[OCSSOrderModel]?;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.automaticallyAdjustsScrollViewInsets = false;
        self.regTableViewNib(tableView: tableView, className: OCSSOrderCell.className());
        requestFotOrderData();
    }
    //刷新
    override func refresh() {
        self.page = 1;
        requestFotOrderData();
    }
    override func loadMore() {
        self.page += 1;
        requestFotOrderData();
    }
    //    MARK:请求订单数据
    func requestFotOrderData() -> Void {
        let params = ["pageIndex":self.page]
        
        OCSSNetRequest.requestData(url: "/Order/OrderList_app", para: params, type: MethodType.post, success: { (result) in
            let resDict = JSON(result).dictionaryValue;
            let arr = JSON(resDict["Data"]!["Items"]).arrayObject! as NSArray;
            let orders = ([OCSSOrderModel].deserialize(from: arr))! as? [OCSSOrderModel];
            if self.page == 1 {
                self.orderData = orders;
            }else{
                self.orderData = orders!+self.orderData!;
            }
            self.tableView.reloadData();
        }) { (error) in
            print(error);
        }
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        if orderData != nil {
            return (self.orderData?.count)!;
        }
        return 0;
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1;
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:OCSSOrderCell = tableView.dequeueReusableCell(withIdentifier: String(describing: OCSSOrderCell.classForCoder())) as! OCSSOrderCell;
        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        cell.model = self.orderData?[indexPath.row];
        
        return cell;
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100;
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 14;
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model:OCSSOrderModel = self.orderData![indexPath.row];
        let orderDetailPage = OCSSRetailOrderDetail.init();
        orderDetailPage.hidesBottomBarWhenPushed = true;
        orderDetailPage.orderType = model.OrderType;
        orderDetailPage.orderId = model.ID;
        navigationController?.pushViewController(orderDetailPage, animated: true);
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
